package com.example.administrator.swredeem.activity;

import android.arch.lifecycle.Observer;
import android.content.Intent;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.swredeem.R;
import com.example.administrator.swredeem.appBase.Config;
import com.example.administrator.swredeem.beanClass.UserListBean;
import com.example.administrator.swredeem.net.HttpXutils;
import com.example.administrator.swredeem.net.MyCallback;
import com.example.administrator.swredeem.utils.LiveDataBus;

import com.example.administrator.swredeem.utils.Myjson;
import com.example.administrator.swredeem.view.adapter.UserListAdapter;
import com.example.administrator.swredeem.view.recyclerview.RecyclerItemClickSupport;
import com.google.gson.Gson;

import java.util.HashMap;
import java.util.Map;

public class UserList extends AppCompatActivity implements RecyclerItemClickSupport.OnItemClickListener {
    private String title, huhao, idCrad;

    private RecyclerView recyclerView;
    private UserListAdapter userListAdapter;
    private UserListBean userListBean;
    private Gson gson = new Gson();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_list);
        Intent intent = getIntent();
        recyclerView = findViewById(R.id.recyclerView);
        if (getIntent() != null) {
            title = intent.getStringExtra("title");
            huhao = intent.getStringExtra("huhao");
            idCrad = intent.getStringExtra("idCrad");
            if (huhao != null && huhao.length() != 0) {
                init(huhao, "1");
            }else {
                init(idCrad, "2");
            }
        }


    }

    private void init(String qureyid, String type) {
        userListBean = new UserListBean();
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(UserList.this);  //布局管理器
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.hasFixedSize();
        RecyclerItemClickSupport.addTo(recyclerView).setOnItemClickListener(this);

        MyCallback myCallback = new MyCallback() {
            @Override
            public void onSuccess(String result) {
                Log.d("TAG", "onSuccess: " + result);
                userListBean = gson.fromJson(result, UserListBean.class);
                LiveDataBus.get().with("pipo").setValue(userListBean);
                if (userListBean.getInfo() != null && userListBean.getInfo().size() > 0) {
                    userListAdapter = new UserListAdapter(userListBean.getInfo());
                    recyclerView.setAdapter(userListAdapter);
                } else {
                    Toast.makeText(UserList.this, "查无此户", Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Log.d("TAG", "onError: "+ex.getMessage()+isOnCallback);

                Toast.makeText(UserList.this, "网络错误，请联系管理员", Toast.LENGTH_SHORT).show();
            }
        };
        Map<String, String> map = new HashMap<>();
        map.put("spotno", "1");
        map.put("operno", "1");
        map.put("orgcode", "1");
        map.put("dataorgid", "1");
        map.put("type", type+"");
        map.put("qureyid", qureyid+"");
        Log.d("TAG", "init: =" + TextUtils.isEmpty(huhao) + TextUtils.isEmpty(idCrad));
        HttpXutils.getData(Config.ICUser, map, myCallback);

    }


    public void showUserDialog(View v) {
//        showSetDeBugDialog();
    }


    private void showSetDeBugDialog(final UserListBean.InfoBean elementsBean) {
        AlertDialog.Builder setDeBugDialog = new AlertDialog.Builder(this);
        //获取界面
        View dialogView = LayoutInflater.from(this).inflate(R.layout.system_admin_psw_alert_dialog_userlist, null);
        //将界面填充到AlertDiaLog容器
        setDeBugDialog.setView(dialogView);
        //初始化控件
        TextView textview_setname = dialogView.findViewById(R.id.textview_setname);
        TextView tvhuhao = dialogView.findViewById(R.id.dialog_huhao);
        TextView tvname = dialogView.findViewById(R.id.dialog_name);
        TextView tvaddr = dialogView.findViewById(R.id.dialog_addr);
        TextView tvbjtype = dialogView.findViewById(R.id.dialog_bjtype);
        TextView tvidCrad = dialogView.findViewById(R.id.dialog_idCrad);
        tvhuhao.setText(elementsBean.getUserid());
        tvname.setText(elementsBean.getUsername());
        tvaddr.setText(elementsBean.getAddr());
        tvbjtype.setText(elementsBean.getRestype());
        tvidCrad.setText(elementsBean.getCertnum());


        Button okButton = (Button) dialogView.findViewById(R.id.queren);
        Button noButton = (Button) dialogView.findViewById(R.id.quxiao);

        if (title.equals("开卡")) {
            textview_setname.setText("是否确认对此户开户？");
        } else {
            textview_setname.setText("是否确认对此户进行补卡？");
        }

        //取消点击外部消失弹窗
        setDeBugDialog.setCancelable(false);
        //创建AlertDiaLog
        setDeBugDialog.create();
        //AlertDiaLog显示
        final AlertDialog customAlert = setDeBugDialog.show();
        //设置自定义界面的点击事件逻辑
        okButton.setOnClickListener(new android.view.View.OnClickListener() {
            @Override
            public void onClick(View view) {
                customAlert.dismiss();
                Intent intent = new Intent(UserList.this, UserDataActivity.class);
                intent.putExtra("title", title);
                intent.putExtra("elementsBean", Myjson.toJson(elementsBean, 1));

//                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);

            }
        });
        noButton.setOnClickListener(new android.view.View.OnClickListener() {
            @Override
            public void onClick(View view) {
                customAlert.dismiss();
            }
        });
    }


    @Override
    public void onItemClicked(RecyclerView recyclerView, int position, View v) {
        showSetDeBugDialog(userListBean.getInfo().get(position));
    }
}
